package cn.com.surker.util;

import java.util.Date;
import java.util.Random;

/**
 * @Author lucky
 * @create 2023年04月20 9:01
 * @desc
 */
public class OrderNoUtil {

    /**
     * 生产任务单号生成规则
     * @param station 公司名称
     * @param flagType 前缀类型  微信订单传入w
     * @return
     */
    public static String initIndentCode(String station, String flagType) {
        if (StringUtils.isAllParamNull(station))
            return "";
        if (StringUtils.isNull(flagType))
            flagType = "";
        String prefix = ChineseCharacterUtil.convertHanzi2Pinyin(station, false).toUpperCase();
        if (prefix.length() > 3)
            prefix = prefix.substring(0, 3);
        prefix = flagType + "R" + prefix;
        Date nowDate = new Date();
        String ymdh = DateUtil.convertString(nowDate, "yyMMdd");
        prefix += "-" + ymdh;
        String s = DateUtil.convertString(nowDate, "HHmmss");
        prefix += "-" + s;
        return prefix;
    }
    /**
     * 无人过磅订单生成规则
     * @param station
     * @param phone
     * @return
     */
    public static String getOrderNo(String station, String phone) {
        if (StringUtils.isAllParamNull(station))
            return "";
        Random random = new Random();
        int num = random.nextInt(8999) + 1000;
        String prefix = ChineseCharacterUtil.convertHanzi2Pinyin(station, false).toUpperCase();
        Date nowDate = new Date();
        String year = DateUtil.convertString(nowDate, "yyyy").substring(2);
        String orderNo = prefix + "-" + year + "-" + DateUtil.convertString(nowDate, "MMdd")
                + "-" + DateUtil.convertString(nowDate, "HHmmss") + "-" + num;
        return orderNo;
    }
    public static String getOrderNo(String station, String phone, String time) {
        if (StringUtils.isAllParamNull(station))
            return "";
        Random random = new Random();
        int num = random.nextInt(8999) + 1000;
        String prefix = ChineseCharacterUtil.convertHanzi2Pinyin(station, false).toUpperCase();
        Date nowDate = new Date();
        if(!StringUtils.isNull(time))
            nowDate = DateUtil.convertDate(time);
        String year = DateUtil.convertString(nowDate, "yyyy").substring(2);
        String orderNo = prefix + "-" + year + "-" + DateUtil.convertString(nowDate, "MMdd")
                + DateUtil.convertString(nowDate, "HHmm") + "-" + num;
        return orderNo;
    }

    /**
     * 客户编号生成
     * @param id
     * @return
     */
    public static String getClientNo(Long id) {
        Date nowDate = new Date();
        String ids = "";
        if (id < 1000 && id > 100)
            ids = "0" + id;
        else if (id < 100 && id > 10)
            ids = "00" + id;
        else if (id < 10 && id > 0)
            ids = "0" + id;
        else
            ids = id + "";

        String year = DateUtil.convertString(nowDate, "yyyy-MMdd") + "-";
        String orderNo = "SC" + "-" + year + ids;
        return orderNo;
    }

    /**
     * 材料合同编号
     * @param id
     * @param station
     * @return
     */
    public static String materialContractNo(Long id, String station) {
        Date nowDate = new Date();
        String prefix = ChineseCharacterUtil.convertHanzi2Pinyin(station, false).toUpperCase();
        if (prefix.length() > 4)
            prefix = prefix.substring(0, 4);
        String code = "M" + prefix + "-" + DateUtil.convertString(nowDate, "yyyy");
        if (id < 100)
            code += "-000" + id;
        else if (id < 1000)
            code += "-00" + id;
        else if (id < 10000)
            code += "-0" + id;
        else
            code += "-" + id;
        return code;
    }

    /**
     * 上传配合比订单号
     * @param id
     * @param station
     * @return
     */
    public static String labUploadNo(Long id, Long lineId, String station) {
        Date nowDate = new Date();
        String prefix = ChineseCharacterUtil.convertHanzi2Pinyin(station, false).toUpperCase();
        if (prefix.length() > 4)
            prefix = prefix.substring(0, 4);
        String code = "S" + lineId + prefix + "-" + DateUtil.convertString(nowDate, "yyyy").substring(2) + "-" + DateUtil.convertString(nowDate, "MMdd");
        if (id < 100)
            code += "-000" + id;
        else if (id < 1000)
            code += "-00" + id;
        else if (id < 10000)
            code += "-0" + id;
        else
            code += "-" + id;
        return code;
    }
    /**
     * 生成销售合同编号
     * @param id
     * @param quality 性质(1:正式2:临时)
     * @param workNature 供应性质(0：销售:1：内部代加工、2：外包代加工)
     * @param station
     * @return
     */
    public static String getSaleContractNo(Long id, Integer quality, Integer workNature, String station) {
        Date nowDate = new Date();
        String ids = "";
        if (id < 1000 && id > 100)
            ids = "0" + id;
        else if (id < 100 && id > 10)
            ids = "00" + id;
        else if (id < 10 && id > 0)
            ids = "0" + id;
        else
            ids = id + "";

        String prefix = ChineseCharacterUtil.convertHanzi2Pinyin(station, false).toUpperCase();
        if (prefix.length() > 4)
            prefix = prefix.substring(0, 4);
        if (workNature.intValue() == 1) {
            if (quality.intValue() == 1)
                prefix = "HW" + prefix;
            else
                prefix = "LW" + prefix;
        } else if (workNature.intValue() == 2) {
            if (quality.intValue() == 1)
                prefix = "HE" + prefix;
            else
                prefix = "LE" + prefix;
        } else {
            if (quality.intValue() == 1)
                prefix = "H" + prefix;
            else
                prefix = "L" + prefix;
        }

        String year = DateUtil.convertString(nowDate, "yyyy");
        String orderNo = prefix + "-" + year + "-"+ ids;
        return orderNo;
    }

}
